package ccgl.com.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import ccgl.com.dao.common.BaseDao;
import ccgl.com.factory.ConnectionFactory;
import ccgl.com.model.MKh;

/**
 * 商品管理Dao
 * 
 * @author Administrator
 * 
 */
public class KhglDao extends BaseDao<MKh> {

	public boolean addKhxx(MKh kh) throws SQLException {
		String sql = "insert into khb(id,xm,qq,dh,xb,nl,dz,bz,userid,lrsj) values(?,?,?,?,?,?,?,?,?,?)";
		Connection con = ConnectionFactory.getConnection();
		try {
			if (super.update(sql, con, kh.getPropertiesArray()) > 0) {
				return true;
			}
			
		} finally {
			con.close();
		}
		return false;
	}

	public boolean updateKhxx(MKh kh) throws SQLException {
		String sql = "update khb set  xm=?,qq=?,dh=?,xb=?,nl=?,dz=?,bz=?,lrsj=? where id='" + kh.getId() + "'";
		Connection con = ConnectionFactory.getConnection();
		try {
			if (super.update(sql, con, kh.getPropertiesUpdateArray()) > 0) {
				return true;
			}
		} finally {
			con.close();
		}
		return false;
	}


	public Map getKhList(int star, int end, String userid) throws SQLException {
		String sql = "SELECT * FROM khb where userid='" + userid + "' LIMIT " + star + "," + end;
		String countSql = "select count(*) from khb where userid='" + userid + "'";
		return getKhList(sql, countSql);
	}
	
	public Map getKhList(String sql, String countSql) throws SQLException {
		Map map = new HashMap();
		Connection con = ConnectionFactory.getConnection();
		try {
			map.put("Rows", super.queryForList(sql, con));
			map.put("Total", super.getCount(countSql, con));
		} finally {
			con.close();
		}
		return map;
	}

	public boolean deleteKhxxs(String idStr) throws SQLException {
		String sql = "delete from khb  where id in (" + idStr + ")";
		Connection con = ConnectionFactory.getConnection();
		try {
			if(super.update(sql, con) > 0) {
				return true;
			}
		} finally {
			con.close();
		}
		return false;
	}

}
